{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "06187d21-97f0-4ed6-ba27-cda7c9ca4ac4",
   "metadata": {},
   "source": [
    "# Python - 函数与文件"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "557b74b1-62f2-4464-b562-c0a705c671d3",
   "metadata": {},
   "source": [
    "## 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "39c0cfcf-6025-4682-8c1c-a7439b54eed9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<svg width=\"480\" height=\"480\" style=\"fill:none; stroke-linecap:round;\">\n",
       "    <rect width=\"100%\" height=\"100%\" fill=\"#F3F3F7\" />\n",
       "\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 240,240\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 100,100 150,100 150,50 100,50 100,100\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 300,300 300,350 350,350 350,300 300,300\" />'\n",
       "\n",
       "<g transform=\"rotate(90.0,300.0,300.0) translate(300.0, 300.0)\">\n",
       "    <circle stroke=\"#63A375\" stroke-width=\"2\" fill=\"transparent\" r=\"5.5\" cx=\"0\" cy=\"0\"/>\n",
       "    <polygon points=\"0,12 2,9 -2,9\" style=\"fill:#63A375;stroke:#63A375;stroke-width:2\"/>\n",
       "</g>\n",
       "\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import jupyturtle as t\n",
    "#from jupyturtle import *\n",
    "\n",
    "t.make_turtle(delay=0.1, animate=True, height=480, width=480)\n",
    "t.jump_to(100,100)\n",
    "t.fd(50)\n",
    "t.lt(90)\n",
    "t.fd(50)\n",
    "t.lt(90)\n",
    "t.fd(50)\n",
    "t.lt(90)\n",
    "t.fd(50)\n",
    "\n",
    "t.jump_to(300,300)\n",
    "t.fd(50)\n",
    "t.lt(90)\n",
    "t.fd(50)\n",
    "t.lt(90)\n",
    "t.fd(50)\n",
    "t.lt(90)\n",
    "t.fd(50)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ed8ad123-2cb2-4a91-bdb4-62f59a963963",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<svg width=\"480\" height=\"480\" style=\"fill:none; stroke-linecap:round;\">\n",
       "    <rect width=\"100%\" height=\"100%\" fill=\"#F3F3F7\" />\n",
       "\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 240,240\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 100,100 150,100 150,70 100,70 100,100\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 300,300 350,300 350,270 300,270 300,300\" />'\n",
       "\n",
       "<g transform=\"rotate(-90.0,300.0,300.0) translate(300.0, 300.0)\">\n",
       "    <circle stroke=\"#63A375\" stroke-width=\"2\" fill=\"transparent\" r=\"5.5\" cx=\"0\" cy=\"0\"/>\n",
       "    <polygon points=\"0,12 2,9 -2,9\" style=\"fill:#63A375;stroke:#63A375;stroke-width:2\"/>\n",
       "</g>\n",
       "\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import jupyturtle as t\n",
    "t.make_turtle(delay=0.1, animate=True, height=480, width=480)\n",
    "def draw_rectangle(x,y,width,height):\n",
    "    t.jump_to(x,y)\n",
    "    for i in range(4):\n",
    "        if i==0 or i==2:\n",
    "            t.fd(width)\n",
    "        else:\n",
    "            t.fd(height)\n",
    "        t.lt(90)\n",
    "\n",
    "draw_rectangle(100,100,50,30)\n",
    "draw_rectangle(300,300,50,30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9fa70e49-0d3d-4463-9c2a-43cb4b0c9b8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<svg width=\"480\" height=\"480\" style=\"fill:none; stroke-linecap:round;\">\n",
       "    <rect width=\"100%\" height=\"100%\" fill=\"#F3F3F7\" />\n",
       "\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 240,240\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 85.9,162.9 314.1,162.9 129.5,297.1 200,80 270.5,297.1 85.9,162.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 200,200\" />'\n",
       "\n",
       "<g transform=\"rotate(-90.0,200,200) translate(200, 200)\">\n",
       "    <circle stroke=\"#63A375\" stroke-width=\"2\" fill=\"transparent\" r=\"5.5\" cx=\"0\" cy=\"0\"/>\n",
       "    <polygon points=\"0,12 2,9 -2,9\" style=\"fill:#63A375;stroke:#63A375;stroke-width:2\"/>\n",
       "</g>\n",
       "\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import jupyturtle as t\n",
    "t.make_turtle(delay=0.1, animate=True, height=480, width=480)\n",
    "import math\n",
    "def draw_pentagram(x,y,r):\n",
    "    t.jump_to(x-(r*math.cos(18*3.1415/180)),y-(r*math.sin(18*3.1415/180)))\n",
    "    for j in range(5):\n",
    "        t.fd(2*r*math.cos(18*3.1415/180))\n",
    "        t.rt(144)\n",
    "    t.jump_to(x,y)\n",
    "\n",
    "draw_pentagram(200,200,120)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "faa98a4b-fb1f-47b1-84f5-b9aaf11979e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<svg width=\"480\" height=\"480\" style=\"fill:none; stroke-linecap:round;\">\n",
       "    <rect width=\"100%\" height=\"100%\" fill=\"#F3F3F7\" />\n",
       "\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 240,240\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 10,200 250,200 250,10 10,10 10,200\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 15.5,21.9 34.5,21.9 19.1,33.1 25,15 30.9,33.1 15.5,21.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 25,25\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 55.5,21.9 74.5,21.9 59.1,33.1 65,15 70.9,33.1 55.5,21.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 65,25\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 95.5,21.9 114.5,21.9 99.1,33.1 105,15 110.9,33.1 95.5,21.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 105,25\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 135.5,21.9 154.5,21.9 139.1,33.1 145,15 150.9,33.1 135.5,21.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 145,25\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 175.5,21.9 194.5,21.9 179.1,33.1 185,15 190.9,33.1 175.5,21.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 185,25\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 215.5,21.9 234.5,21.9 219.1,33.1 225,15 230.9,33.1 215.5,21.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 225,25\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 33,41.9 52,41.9 36.6,53.1 42.5,35 48.4,53.1 33,41.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 42.5,45\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 73,41.9 92,41.9 76.6,53.1 82.5,35 88.4,53.1 73,41.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 82.5,45\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 113,41.9 132,41.9 116.6,53.1 122.5,35 128.4,53.1 113,41.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 122.5,45\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 153,41.9 172,41.9 156.6,53.1 162.5,35 168.4,53.1 153,41.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 162.5,45\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 193,41.9 212,41.9 196.6,53.1 202.5,35 208.4,53.1 193,41.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 202.5,45\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 15.5,61.9 34.5,61.9 19.1,73.1 25,55 30.9,73.1 15.5,61.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 25,65\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 55.5,61.9 74.5,61.9 59.1,73.1 65,55 70.9,73.1 55.5,61.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 65,65\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 95.5,61.9 114.5,61.9 99.1,73.1 105,55 110.9,73.1 95.5,61.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 105,65\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 135.5,61.9 154.5,61.9 139.1,73.1 145,55 150.9,73.1 135.5,61.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 145,65\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 175.5,61.9 194.5,61.9 179.1,73.1 185,55 190.9,73.1 175.5,61.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 185,65\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 215.5,61.9 234.5,61.9 219.1,73.1 225,55 230.9,73.1 215.5,61.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 225,65\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 33,81.9 52,81.9 36.6,93.1 42.5,75 48.4,93.1 33,81.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 42.5,85\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 73,81.9 92,81.9 76.6,93.1 82.5,75 88.4,93.1 73,81.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 82.5,85\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 113,81.9 132,81.9 116.6,93.1 122.5,75 128.4,93.1 113,81.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 122.5,85\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 153,81.9 172,81.9 156.6,93.1 162.5,75 168.4,93.1 153,81.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 162.5,85\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 193,81.9 212,81.9 196.6,93.1 202.5,75 208.4,93.1 193,81.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 202.5,85\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 15.5,101.9 34.5,101.9 19.1,113.1 25,95 30.9,113.1 15.5,101.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 25,105\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 55.5,101.9 74.5,101.9 59.1,113.1 65,95 70.9,113.1 55.5,101.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 65,105\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 95.5,101.9 114.5,101.9 99.1,113.1 105,95 110.9,113.1 95.5,101.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 105,105\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 135.5,101.9 154.5,101.9 139.1,113.1 145,95 150.9,113.1 135.5,101.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 145,105\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 175.5,101.9 194.5,101.9 179.1,113.1 185,95 190.9,113.1 175.5,101.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 185,105\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 215.5,101.9 234.5,101.9 219.1,113.1 225,95 230.9,113.1 215.5,101.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 225,105\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 33,121.9 52,121.9 36.6,133.1 42.5,115 48.4,133.1 33,121.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 42.5,125\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 73,121.9 92,121.9 76.6,133.1 82.5,115 88.4,133.1 73,121.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 82.5,125\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 113,121.9 132,121.9 116.6,133.1 122.5,115 128.4,133.1 113,121.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 122.5,125\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 153,121.9 172,121.9 156.6,133.1 162.5,115 168.4,133.1 153,121.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 162.5,125\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 193,121.9 212,121.9 196.6,133.1 202.5,115 208.4,133.1 193,121.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 202.5,125\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 15.5,141.9 34.5,141.9 19.1,153.1 25,135 30.9,153.1 15.5,141.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 25,145\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 55.5,141.9 74.5,141.9 59.1,153.1 65,135 70.9,153.1 55.5,141.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 65,145\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 95.5,141.9 114.5,141.9 99.1,153.1 105,135 110.9,153.1 95.5,141.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 105,145\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 135.5,141.9 154.5,141.9 139.1,153.1 145,135 150.9,153.1 135.5,141.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 145,145\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 175.5,141.9 194.5,141.9 179.1,153.1 185,135 190.9,153.1 175.5,141.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 185,145\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 215.5,141.9 234.5,141.9 219.1,153.1 225,135 230.9,153.1 215.5,141.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 225,145\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 33,161.9 52,161.9 36.6,173.1 42.5,155 48.4,173.1 33,161.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 42.5,165\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 73,161.9 92,161.9 76.6,173.1 82.5,155 88.4,173.1 73,161.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 82.5,165\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 113,161.9 132,161.9 116.6,173.1 122.5,155 128.4,173.1 113,161.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 122.5,165\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 153,161.9 172,161.9 156.6,173.1 162.5,155 168.4,173.1 153,161.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 162.5,165\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 193,161.9 212,161.9 196.6,173.1 202.5,155 208.4,173.1 193,161.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 202.5,165\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 15.5,181.9 34.5,181.9 19.1,193.1 25,175 30.9,193.1 15.5,181.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 25,185\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 55.5,181.9 74.5,181.9 59.1,193.1 65,175 70.9,193.1 55.5,181.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 65,185\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 95.5,181.9 114.5,181.9 99.1,193.1 105,175 110.9,193.1 95.5,181.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 105,185\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 135.5,181.9 154.5,181.9 139.1,193.1 145,175 150.9,193.1 135.5,181.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 145,185\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 175.5,181.9 194.5,181.9 179.1,193.1 185,175 190.9,193.1 175.5,181.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 185,185\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 215.5,181.9 234.5,181.9 219.1,193.1 225,175 230.9,193.1 215.5,181.9\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 225,185\" />'\n",
       "\n",
       "<g transform=\"rotate(-90.0,225,185) translate(225, 185)\">\n",
       "    <circle stroke=\"#63A375\" stroke-width=\"2\" fill=\"transparent\" r=\"5.5\" cx=\"0\" cy=\"0\"/>\n",
       "    <polygon points=\"0,12 2,9 -2,9\" style=\"fill:#63A375;stroke:#63A375;stroke-width:2\"/>\n",
       "</g>\n",
       "\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import jupyturtle as t\n",
    "t.make_turtle(delay=0.1, animate=True, height=480, width=480)\n",
    "import math\n",
    "def draw_rectangle(x,y,width,height):\n",
    "    t.jump_to(x,y)\n",
    "    for i in range(4):\n",
    "        if i==0 or i==2:\n",
    "            t.fd(width)\n",
    "        else:\n",
    "            t.fd(height)\n",
    "        t.lt(90)\n",
    "\n",
    "def draw_pentagram(x,y,r):\n",
    "    t.jump_to(x-(r*math.cos(18*3.1415/180)),y-(r*math.sin(18*3.1415/180)))\n",
    "    for j in range(5):\n",
    "        t.fd(2*r*math.cos(18*3.1415/180))\n",
    "        t.rt(144)\n",
    "    t.jump_to(x,y)\n",
    "\n",
    "draw_rectangle(10,200,240,190)\n",
    "for i in range(9):\n",
    "    if i%2==0:\n",
    "        for j in range(6):\n",
    "            draw_pentagram(25+j*40,25+i*20,10)\n",
    "    else:\n",
    "        for k in range(5):\n",
    "            draw_pentagram(42.5+k*40,25+i*20,10)\n",
    "            \n",
    "            \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "05059a56-bcfc-43bf-a43c-73942d41b0b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "def add(x,y=1):\n",
    "    # Default,缺省值\n",
    "    return x+y\n",
    "\n",
    "print(add(3)) #正常下需给两个值3被自动赋值给x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2fe4e1c-9310-42aa-9338-4b7967f157b9",
   "metadata": {},
   "source": [
    "# 文件"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bd9a838-55b7-4208-8bf4-498de24d32d5",
   "metadata": {},
   "source": [
    "### 数据处理角度来说，文件分成两种：二进制文件，文本文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9602cec9-1888-4ead-a8e8-5b4aa8401c1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,2,3,4\n",
      "2,3,4,5\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "file = open(\"text.txt\",\"r\")\n",
    "content = file.read()\n",
    "file.close()\n",
    "print(content)\n",
    "print(type(content))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fdf933de-a2fe-4e5b-a33e-876fee18a833",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2,3,4,5\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "file = open(\"text.txt\",\"r\")\n",
    "line = file.readlines()\n",
    "file.close()\n",
    "print(line[1])\n",
    "print(type(line))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5494eba8-6f2d-4aa2-84cb-ffed50bc89ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "with open(\"text.txt\",\"r\") as file: #用这种方法写文件会自动关闭\n",
    "    lines = file.readlines()\n",
    "print(type(lines))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2a450da5-4a6f-46d1-bc7a-1929793c5425",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"text.txt\",\"a\") as file: \n",
    "    file.write(\"1,3,5,7,9\\n\")\n",
    "    file.write(\"2,4,6,8,0\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4d3bd19c-0e3b-469e-991f-6ef516d705e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 92.4185 , 93.2367 , 51.5826 , 59.5908 , 37.5301 , 14.6008 , 68.5995 , 24.1725 ,  8.2266 , 87.7224 \n",
      "  5.2995 ,  5.9839 , 99.0513 , 49.7637 , 37.6993 , 99.1523 , 36.7169 , 83.7957 , 94.8332 , 76.3707 \n",
      " 45.2260 , 15.9240 , 29.8647 , 86.5228 , 62.2742 , 93.6401 , 68.4527 , 95.4431 , 57.6197 , 23.5706 \n",
      " 35.4783 ,  3.8133 , 10.0999 , 66.0248 , 37.5453 , 58.2346 , 62.3246 , 33.5184 , 26.1691 , 51.0522 \n",
      " 37.2034 , 66.9399 ,  2.8564 , 61.9150 , 20.3013 , 82.1176 , 15.6730 , 61.9866 , 91.3768 , 28.2320 \n",
      " 59.0098 , 14.0788 , 67.8655 , 93.6442 , 92.1367 , 95.3605 , 96.3669 , 58.7685 , 25.5198 , 10.6397 \n",
      " 69.7658 , 63.6637 , 78.3211 , 36.9210 , 70.0980 , 93.6427 , 77.4986 , 14.9585 , 88.4368 , 98.8784 \n",
      " 20.7787 , 23.6365 , 49.1054 , 51.1473 , 35.8902 ,  7.6260 , 33.0149 , 47.0731 , 28.9613 , 27.8418 \n",
      " 28.6005 , 22.6277 , 91.5943 , 71.9630 , 45.0963 , 71.8280 , 40.6284 , 97.2972 , 64.1557 , 68.8255 \n",
      " 75.4935 ,  5.2699 , 21.4237 , 84.3501 ,  3.6848 ,  4.0519 , 62.3926 , 51.1113 , 61.2592 , 29.7359 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import random\n",
    "with open(\"text.txt\",\"w\") as file: \n",
    "    for i in range(10):\n",
    "        for j in range(9):\n",
    "            a = random.uniform(0,100)\n",
    "            file.write(\"%8.4f\"%a+\" ,\")\n",
    "            if j==8:\n",
    "                a = random.uniform(0,100)\n",
    "                file.write(\"%8.4f\"%a+\" \\n\")\n",
    "\n",
    "with open(\"text.txt\",\"r\") as file:\n",
    "    content = file.read()\n",
    "    print(content)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b00fbf76-9260-4169-a1b4-4ac3f880d188",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 10) 的数据个数是: 9\n",
      "[10, 20) 的数据个数是: 7\n",
      "[20, 30) 的数据个数是: 15\n",
      "[30, 40) 的数据个数是: 10\n",
      "[40, 50) 的数据个数是: 6\n",
      "[50, 60) 的数据个数是: 9\n",
      "[60, 70) 的数据个数是: 15\n",
      "[70, 80) 的数据个数是: 7\n",
      "[80, 90) 的数据个数是: 6\n",
      "[90, 100) 的数据个数是: 16\n"
     ]
    }
   ],
   "source": [
    "numbers = []\n",
    "\n",
    "with open(\"text.txt\", \"r\") as file:\n",
    "    for line in file:\n",
    "        number_strings = line.strip().split(', ')\n",
    "        numbers.extend([float(num) for num in number_strings]) \n",
    "\n",
    "for i in range(0,100,10):\n",
    "    count=0\n",
    "    for num in numbers:\n",
    "        if i<= num <i+10:\n",
    "            count+=1\n",
    "    print(f\"[{i}, {i+10}) 的数据个数是: {count}\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "6ac80f01-bccf-4744-9879-0fc8b059dbd2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "316ba13d-e14a-406d-9965-26962d7362c8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
